package com.bootdo.system.service.impl;

import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.TMagazinePicturesDao;
import com.bootdo.system.domain.TMagazinePicturesDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.TMagazineDao;
import com.bootdo.system.domain.TMagazineDO;
import com.bootdo.system.service.TMagazineService;
import org.springframework.util.CollectionUtils;


@Service
public class TMagazineServiceImpl implements TMagazineService {
	private static final Logger logger = LoggerFactory.getLogger(TMagazineServiceImpl.class);
	@Autowired
	private TMagazineDao tMagazineDao;
	@Autowired
	private TMagazinePicturesDao tMagazinePicturesDao;
	
	@Override
	public TMagazineDO get(Integer id){
		return tMagazineDao.get(id);
	}
	
	@Override
	public List<TMagazineDO> list(Map<String, Object> map){
		return tMagazineDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return tMagazineDao.count(map);
	}
	
	@Override
	public int save(TMagazineDO magazine){
		if (null!=magazine.getId()){
			magazine.setStatus("1");
			return tMagazineDao.update(magazine);
		}else {
			try {
				magazine.setIsValid(false);
				magazine.setStatus("0");
				magazine.setIsInBooklist(false);
				magazine.setCreatedTime(new Date());
				return tMagazineDao.save(magazine);
			} catch (DuplicateKeyException e) {
				logger.error("-------------------issn already exist----------------");
				return -1;
			}

		}
	}
	
	@Override
	public int update(TMagazineDO tMagazine){
		return tMagazineDao.update(tMagazine);
	}
	
	@Override
	public int remove(Integer id){
		return tMagazineDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return tMagazineDao.batchRemove(ids);
	}

	@Override
	public int verify(TMagazineDO magazineDO) {
		magazineDO.setVerifyTime(new Date());
		return tMagazineDao.update(magazineDO);
	}

	@Override
	public String changeTitlePage(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("limit", 1);
		params.put("offset", 0);
		params.put("sort", "page_no");
		params.put("order", "asc");
		params.put("magazineId", id);
		Query query = new Query(params);
		List<TMagazinePicturesDO> magazines = tMagazinePicturesDao.list(query);
		TMagazineDO tMagazine = new TMagazineDO();
		tMagazine.setId(id);
		if (!CollectionUtils.isEmpty(magazines)) {
			tMagazine.setTitlePage(magazines.get(0).getPicture());
		} else {
			tMagazine.setTitlePage("");
		}
		tMagazineDao.update(tMagazine);
		return tMagazine.getTitlePage();
	}

	@Override
	public Boolean isExistence(TMagazineDO tMagazineDO) {
		if (null!=tMagazineDO.getId() && tMagazineDO.getId()>0){
			TMagazineDO magazineDO = tMagazineDao.get(tMagazineDO.getId());
			if (null!=magazineDO && StringUtils.isNotBlank(magazineDO.getIssn())){
				if (tMagazineDO.getIssn().equalsIgnoreCase(magazineDO.getIssn())){
					return false;
				}else {
					return getaBoolean(tMagazineDO);
				}
			}
			return false;
		}else {
			return getaBoolean(tMagazineDO);
		}
	}

	private Boolean getaBoolean(TMagazineDO tMagazineDO) {
		Map<String, Object> params = new HashMap<>();
		params.put("issn", tMagazineDO.getIssn());
		params.put("limit", 1);
		params.put("offset", 0);
		Query query = new Query(params);
		List<TMagazineDO> magazines = tMagazineDao.list(query);
		if (CollectionUtils.isEmpty(magazines)) {
			return false;
		}
		return true;
	}

}
